package cn.ssm.test.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.ssm.test.model.User;
import cn.ssm.test.service.IUserService;
import cn.ssm.test.utils.JedisClient;
import cn.ssm.test.utils.JsonUtils;    
    
@Controller    
@RequestMapping("/user")    
public class UserController {    
    @Resource    
    private IUserService userService;    
    @Autowired
    private JedisClient jedisClient;
    
    @RequestMapping("/login")    
    public String login(HttpServletRequest request,Model model){    
       String name = request.getParameter("name");
       String password = request.getParameter("password");
       User user = userService.getUserByName(name);
    	if (user != null && user.getPassword().equals(password)) {
			request.getSession().setAttribute("user", JsonUtils.objectToJson(user));
			model.addAttribute("user", user);
    	}
    	
        return "indexuser";    
    }    
    @RequestMapping("/index")    
    public String index(HttpServletRequest request,Model model){    
    	
    	
    	
    	return "index";    
    }    
    @RequestMapping("/SSOLogin")    
    public String SSOLogin(HttpServletRequest request,Model model){    
    	String token = request.getParameter("token");
		//到redis中查询用户信息
		String json = jedisClient.get("REDIS_SESSION" + ":" + token);
		User user = JSON.parseObject(json, User.class);
		model.addAttribute("user", user);
    	return "index";    
    }    
    @RequestMapping("/userinfo")    
    public String userInfo(HttpServletRequest request,Model model){
    	if (request.getSession().getAttribute("user") != null) {
    		System.err.println(request.getSession().getAttribute("user")+"8082");
    		User user = JsonUtils.jsonToPojo(request.getSession().getAttribute("user").toString(), User.class);
    		model.addAttribute("user", user);
		}
    	return "indexuser";    
    }    
        
    @RequestMapping("/showUser")    
    public String showUser(HttpServletRequest request,Model model){    
        int userId = Integer.parseInt(request.getParameter("id"));    
        User user = userService.getUserById(userId);    
        model.addAttribute("user", user);    
        return "showUser";    
    }    
        
    @RequestMapping("/addUserUI")    
    public String addUserUI(){    
        return "addUser";    
    }    
        
    @RequestMapping("/addUser")    
    public String addUser(HttpServletRequest request,Model model){    
        User user = new User();    
        user.setName(String.valueOf(request.getParameter("name")));    
        user.setPassword(String.valueOf(request.getParameter("password")));    
       // user.setAge(Integer.parseInt(String.valueOf(request.getParameter("age"))));    
        userService.addUser(user);    
        return "redirect:/user/userList";    
    }    
}
